/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadCargo.java
 *
 * Created on 13/09/2011, 21:17:28
 */
package visao;

import cargo.Cargo;
import fachada.FachadaBDR;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;

/**
 *
 * @author Administrador
 */
public class CadCargo extends javax.swing.JFrame {

    /** Creates new form CadCargo */
    public CadCargo() {
        initComponents();

        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Cargo.gif")).getImage());

    }

    /** Creates new form CadCargo */
    public CadCargo(int idCargo) throws Exception {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Cargo.gif")).getImage());
        try {
            // recuperando a instancia da fachada
            FachadaBDR objfaFachadaBDR = FachadaBDR.getInstancia();
            // recuperando o cargo 
            Cargo cargo = objfaFachadaBDR.procurarCargo(idCargo);
            // setando os valores
            jTextField_DescricaoCargo.setText(cargo.getDescricaoCargo());
            jCheckBox_StatusCargo.setSelected(cargo.getStatusCargo());
            jTextField_idCodigo.setText(String.valueOf(cargo.getIdCargo()));

        } catch (CamposObrigatoriosException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }

        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Cargo.gif")).getImage());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_IdCargo = new javax.swing.JLabel();
        jLabel_DescricaoCargo = new javax.swing.JLabel();
        jTextField_DescricaoCargo = new javax.swing.JTextField();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jCheckBox_StatusCargo = new javax.swing.JCheckBox();
        jLabel_Mensagem = new javax.swing.JLabel();
        jTextField_idCodigo = new javax.swing.JTextField();

        setTitle("Cargo");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel_IdCargo.setText("Codigo :");

        jLabel_DescricaoCargo.setText("Descrição :");

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Carcelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jCheckBox_StatusCargo.setSelected(true);
        jCheckBox_StatusCargo.setText("Ativo");
        jCheckBox_StatusCargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_StatusCargoActionPerformed(evt);
            }
        });

        jTextField_idCodigo.setText("0");
        jTextField_idCodigo.setEnabled(false);
        jTextField_idCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_idCodigoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_DescricaoCargo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                    .addComponent(jLabel_IdCargo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_DescricaoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jCheckBox_StatusCargo)
                        .addGap(45, 45, 45))
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25))
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jButton_Ok, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(252, 252, 252))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_IdCargo)
                            .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jCheckBox_StatusCargo)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_DescricaoCargo)
                    .addComponent(jTextField_DescricaoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Ok)
                    .addComponent(jButton_Cancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBox_StatusCargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_StatusCargoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox_StatusCargoActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idCargo = Integer.parseInt(jTextField_idCodigo.getText());
        String descricaoCargo = jTextField_DescricaoCargo.getText();
        Boolean statusCargo = jCheckBox_StatusCargo.isSelected();


        try {

            FachadaBDR objfaFachadaBDR = FachadaBDR.getInstancia();

            Cargo cargo = new Cargo(idCargo, descricaoCargo, rootPaneCheckingEnabled, null);
            // Se n'ao for informado um id eu cadastro o cargo
            if (idCargo == 0) {
                objfaFachadaBDR.cadastrarCargo(cargo);
           
            jTextField_DescricaoCargo.setText("");
            jCheckBox_StatusCargo.setSelected(false);
            
            } else // se não eu atualizo
            {
                objfaFachadaBDR.atualizarCargo(cargo);
            }

            String acao = (idCargo == 0) ? "Inserido" : "Atualizado";

            jLabel_Mensagem.setText("Cargo " + acao + " Com Sucesso!!!");
            jLabel_Mensagem.setForeground(Color.BLACK);

//            if (idCargo == 0) {
//            } else {
//                this.setVisible(false);
//            }


        } catch (CamposObrigatoriosException ex) {
            Logger.getLogger(CadCargo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErroAcessoRepositorioException ex) {
            Logger.getLogger(CadCargo.class.getName()).log(Level.SEVERE, null, ex);
        }





    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:
        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {
            dispose();
        }
    }//GEN-LAST:event_jButton_CancelarActionPerformed

private void jTextField_idCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_idCodigoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField_idCodigoActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
}//GEN-LAST:event_formWindowClosed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    dispose();
//    jButton_Cancelar.doClick();
}//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadCargo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadCargo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadCargo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadCargo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }


        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new CadCargo().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(CadCargo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_StatusCargo;
    private javax.swing.JLabel jLabel_DescricaoCargo;
    private javax.swing.JLabel jLabel_IdCargo;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JTextField jTextField_DescricaoCargo;
    private javax.swing.JTextField jTextField_idCodigo;
    // End of variables declaration//GEN-END:variables
}
